﻿using System;
using System.Net;
using System.Security.Cryptography;

namespace PhiStore
{
    /// <summary>
    /// Object used for containing information broadcasted by PhiServers
    /// </summary>
    [Serializable]
    public class BroadcastObject
    {
        private RSAParameters publicKey;
        private IPAddress iPAddress;
        private int port;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="publicKey"></param>
        /// <param name="IP"></param>
        /// <param name="port"></param>
        public BroadcastObject(RSAParameters publicKey, IPAddress IP, int port)
        {
            this.iPAddress = IP;
            this.port = port;
            this.publicKey = publicKey;
        }

        /// <summary>
        /// Constructor without any public key (usable for comparing to other objects)
        /// </summary>
        /// <param name="IP"></param>
        /// <param name="port"></param>
        public BroadcastObject(IPAddress IP, int port)
        {
            this.publicKey = new RSAParameters();
            this.iPAddress = IP;
            this.port = port;
        }

        /// <summary>
        /// The public key used for encrypting credentials
        /// </summary>
        public RSAParameters PublicKey { get { return publicKey; } }
        
        /// <summary>
        /// The Port
        /// </summary>
        public int Port { get { return port; } }

        /// <summary>
        /// The IP
        /// </summary>
        public IPAddress IP { get { return iPAddress; } }

        /// <summary>
        /// Creates a copy of this object
        /// </summary>
        /// <returns></returns>
        public BroadcastObject Copy()
        {
            return new BroadcastObject(publicKey, iPAddress, port);
        }

        /// <summary>
        /// is IP of this BC a localhost address?
        /// </summary>
        /// <returns></returns>
        public bool IsLocalHost()
        {
            return IPAddress.IsLoopback(iPAddress);
        }

        /// <summary>
        /// self-explanatory
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return iPAddress.ToString() + ":" + port.ToString(); 
        }

        /// <summary>
        /// Equals
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!(obj is BroadcastObject))
                return false;
            else
            {
                BroadcastObject compare = (BroadcastObject)obj;
                bool result = iPAddress.Equals(compare.IP) && port.Equals(compare.Port); // && publicKey.Equals(compare.PublicKey);
                return result;
            }
        }

        /// <summary>
        /// base.GetHashCode()
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode(); 
        }

    }
}
